package com.cskaoyan.gateway.controller.user;


import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IUserService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.dto.UserLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
/*
@RestController
@RequestMapping("user")
public class MyUserLoginController {

    @Reference(timeout = 3000, check = false)
    IUserService service;


    @GetMapping("login")
    @Anoymous
    public String userGetLogin() {
        return "{\"success\":true,\"message\":\"success\",\"code\":200,\"result\":{\"uid\":71,\"username\":\"cskaoyan01\"},\"timestamp\":1642730965281}";
    }

    @Anoymous
    @PostMapping("login")
    public ResponseData userLogin(@RequestBody UserLoginRequest request,
                                  HttpServletRequest servletRequest,
                                  HttpServletResponse servletResponse) {
        final Cookie[] cookies = servletRequest.getCookies();
        if (cookies == null || cookies.length == 0) {
            ResponseData responseData = new ResponseUtil().setErrorMsg("验证码状态异常");
            return responseData;
        }

        // 拿到对应的cookie
        String uuid = CookieUtil.getCookieValue(servletRequest, "kaptcha_uuid");
        UserLoginResponse userLoginResponse = service.userLogin(request, uuid);

        ResponseData response = new ResponseData();
        response.setCode(Integer.parseInt(userLoginResponse.getCode()));
        response.setMessage(userLoginResponse.getMsg());
        response.setTimestamp(System.currentTimeMillis());
        response.setResult(userLoginResponse);

        if (response.getCode() == 200) {
            Cookie cookie = CookieUtil.genCookie("access_token", userLoginResponse.getToken(),
                                                "/",
                                        (int)LocalDateTime.now().plusDays(1).toEpochSecond(ZoneOffset.of("+8")));

            cookie.setHttpOnly(true);
            servletResponse.addCookie(cookie);
            response.setSuccess(true);
        }

        return response;
    }
}
*/